<?php

class Exam_Widget_CategoryController extends Engine_Content_Widget_Abstract
{
	public function indexAction() {
//		// Get category
//		$catTable = Engine_Api::_()->getDbTable('categories', 'exam');
//		$catList = $catTable->select()
//                    ->where('parent_id=0');
//        
////        $catList = $catTable->getCategoryList();
//////        print_r($catList);die;
//		 $this->view->paginator = $paginator = Zend_Paginator::factory($catList);
//

//		$this->view->list_categories = $list_categories = Zend_Registry::get('list_categories');
		$table = Engine_Api::_()->getDbTable('categories', 'exam');
        $classfied_tableName = $table->info('name');
        $select = $table->select()
                    //->where('cat_dependency = ?', 0)
					->order('category_id');

//		$contentTable = Engine_Api::_()->getDbtable('content', 'core');
//		$contentTableName = $contentTable->info('name');
//		$pageTable = Engine_Api::_()->getDbtable('pages', 'core');
//		$pageTableName = $pageTable->info('name');
//		// Make a Widgitized Page (sitepage_index_view) & widgets.
//	
//		$selectPage  = $pageTable->select()
//		->from($pageTableName, array('page_id'))
//		->where('name =?', 'list_index_index')
//		->limit(1);
//	
//		if(!empty($selectPage)) {
//			$pages_id = $selectPage->query()->fetchAll();
//			if(!empty($pages_id)) {			 
//				 $page_id = $pages_id[0]['page_id'];
//				 $contentTablePage  = $contentTable->select()
//																		 ->from($contentTableName, array('page_id'))
//																		 ->where('name =?', 'list.search-list')
//																		 ->where('page_id =?', $page_id)																	 
//																		 ->limit(1);
//				 $contentinfo = $contentTablePage->query()->fetchAll();			
//		     if(!empty($contentinfo)) {
//		    	 $this->view->formsubmit = 1;
//		     } 
//		     else {
//		    	 $this->view->formsubmit = 0;
//		     }
//			}
//		}			
					
					
					
		$categories = array();
		$category_info = $table->fetchAll($select);
		foreach($category_info->toarray() as $value) {
			$sub_cat_array = array();			
			$sub_cat = $table->select()->where('parent_id = ?', $value['category_id'])->order('category_id');
			$category_info2 = $table->fetchAll($sub_cat);
			foreach($category_info2->toarray() as $result_info) {
				$tmp_array = array('sub_cat_id' => $result_info['category_id'],
			                         'sub_cat_name' => $result_info['category_name'],
			                         'order' => $result_info['category_id']);
				$sub_cat_array[] = $tmp_array;
			}
		 	$category_array = array('category_id' => $value['category_id'],
		                        'category_name' => $value['category_name'],
                                'parent_id' => $value['parent_id'],
		                        'order' => $value['category_id'],
		                        'sub_categories' => $sub_cat_array  
		                  );
			$categories[] = $category_array;                  
		}
//        echo '<pre>';
//        print_r($categories);die;
		$this->view->categories1 = $categories;
		$this->view->subcategories = 0;
		$this->view->category = 0;
		
		if(!empty($_GET)) {
	       if(!empty($_GET['subcategory']))
	           $this->view->subcategories = $_GET['subcategory'];
            if(!empty($_GET['category']))
			     $this->view->category		= $_GET['category'];
		} 

		$categoryid = Zend_Controller_Front::getInstance()->getRequest()->getParam('category_id');
		$subcategoryid = Zend_Controller_Front::getInstance()->getRequest()->getParam('subcategory_id');
		if(!empty($categoryid)) {
			$this->view->category	= $categoryid;
			$this->view->subcategories	= $subcategoryid;
		}

	  if (!(count($this->view->categories1) > 0)) {
				return $this->setNoRender();
		}
//		if( empty($list_categories) ) {
//			return $this->setNoRender();
//		}
	}
}
